import { getlist, inster, updates, delet } from '../../api/msbd'

const state = {
    list: [],
    classify: [],
    sort: 'desc',
    orderby: 'id',
    currentPage: 1,
    pageSize: 3,
    total: 0
}

const mutations = {
    SET_LIST(state, data) {
        state.list = data.data
        state.classify = data.classify
        state.total = data.total[0].total
    },
    SET_PAGE(state, aeg) {
        state.currentPage = aeg
    },
    SET_PAGSIZE(state, val) {
        state.pageSize = val
    },
}

const actions = {
    async getList({ commit, state }, text) {
        let data = await getlist(
            {
                sort: state.sort,
                orderby: state.orderby,
                currentPage: state.currentPage - 1,
                pageSize: state.pageSize,
                keyword: text
            }
        )
        commit('SET_LIST', data)
    },
    async addList({ dispatch }, data) {
        await inster(data)
        dispatch('getList')
    },
    async updateList({ dispatch }, data) {
        await updates(data)
        dispatch('getList')
    },
    async deleteList({ dispatch }, id) {
        await delet(id)
        dispatch('getList')
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
